
#include <stdlib.h>
#include <string.h>
#include "lib.h"
#include "serializer.h"		
		
CSerializer::CSerializer(const char *name)
{
	m_serialize_fingerprint = 0;

	FindFingerprint(name);
}

uint32 CSerializer::Serialize(uint8 * /*buffer*/)
{
	return 0;
}

uint32 CSerializer::DeSerialize(uint8 * /*buffer*/)
{
	return 0;
}

void CSerializer::FindFingerprint(const char *name)
{
	int32 namelen = (int32)strlen(name);

	m_serialize_fingerprint = 0;
	for(int32 i = 0; i < namelen; i++)
	{
		m_serialize_fingerprint += name[i];
	}
}

uint32 CSerializer::Wire(uint8 *buffer, uint8 data)
{
	*buffer = data;
	return 1;
}

uint32 CSerializer::Wire(uint8 *buffer, uint16 data)
{
	int i = 0;
	buffer[i++] = (uint8)(data & 0xff);
	buffer[i++] = (uint8)((data >> 8) & 0xff);

	return i;
}

uint32 CSerializer::Wire(uint8 *buffer, uint32 data)
{
	int i = 0;
	buffer[i++] = (uint8)(data & 0xff);
	buffer[i++] = (uint8)((data >> 8) & 0xff);
	buffer[i++] = (uint8)((data >> 16) & 0xff);
	buffer[i++] = (uint8)((data >> 24) & 0xff);

	return i;
}

uint32 CSerializer::Wire(uint8 *buffer, int32 data)
{
	return Wire(buffer, (uint32)data);
}

uint32 CSerializer::Wire(uint8 *buffer, const char *data)
{
	for(uint32 i = 0; i < strlen(data); i++)
	{
		buffer += Wire(buffer, (uint8)data[i]);
	}

	buffer += Wire(buffer, (uint8)'\0');

	return strlen(data) + 1;

}

uint32 CSerializer::Wire(uint8 *buffer, uint8 *data, uint32 size)
{
	memcpy(buffer, data, size);
	return size;
}

uint32 CSerializer::UnWire(uint8 *buffer, uint8 *data)
{
	*data = *buffer;
	return 1;
}

uint32 CSerializer::UnWire(uint8 *buffer, uint16 *data)
{
	*data = (uint16)(
		(buffer[0] & 0xff) |
		((buffer[1] << 8) & 0xff00));

	return 2;
}

uint32 CSerializer::UnWire(uint8 *buffer, uint32 *data)
{
	*data = (uint32)(
		(buffer[0] & 0xff) |
		((buffer[1] << 8) & 0xff00) |
		((buffer[2] << 16) & 0xff0000) |
		((buffer[3] << 24) & 0xff000000));

	return 4;
}

uint32 CSerializer::UnWire(uint8 *buffer, int32 *data)
{
	return UnWire(buffer, (uint32 *)data);
}

uint32 CSerializer::UnWire(uint8 *buffer, char *data)
{
	uint8 *src = buffer;
	while(*src)
	{
		*data = *src;
		src++;
		data++;
	}

	*data = '\0';

	return strlen((char *)buffer) + 1;
}

uint32 CSerializer::UnWire(uint8 *buffer, uint8 *data, uint32 size)
{
	memcpy(data, buffer, size);
	return size;
}







